#include "UtilCommon.h"

namespace Useable
{
	void To_Upper(char* txt)
	{
		ui16 len = strlen(txt);
		for(register ui16 i = 0; i < len; ++i)
			*(txt + i) = toupper(*(txt + i));
	}

	void To_Lower(char* txt)
	{
		ui16 len = strlen(txt);
		for(register ui16 i = 0; i < len; ++i)
			*(txt + i) = tolower(*(txt + i));
	}

	float GetDistance(Object* o1, Object* o2)
	{
		if(!o1 || !o2)
			return 10000.0f;
		Vector3d vec1 = o1->GetPosition();
		Vector3d vec2 = o2->GetPosition();
		return GetDistance(vec1._x, vec1._y, vec1._z, vec2._x, vec2._y, vec2._z);
	}

	float GetDistance(Object* o1, float x, float y, float z)
	{
		Vector3d vec1 = o1->GetPosition();
		return GetDistance(vec1._x, vec1._y, vec1._z, x, y, z);
	}

	float GetDistance(float x1, float y1, float z1, float x2, float y2, float z2)
	{
		float c2 = pow(x1 - x2, 2) + pow(y1 - y2, 2) + pow(z1 - z2, 2);

		//	actually this one should never happen, cause normal algebra-laws dont
		//	allow the power by 2 to be negative. But what, when the laws are wrong? :O
		if(c2 < 0)
			c2 = abs(c2);
		return sqrt(c2);
	}

	ui32 GetTypeFromGuid(ui64& guid)
	{
		return guid & 0xFFF00000;
	}

	ui32 ConvertToCopper(Useable::Money& mon)
	{
		ui32 gold = mon.gold;
		ui32 silver = mon.silver;
		ui32 copper = mon.copper;
		ui32 delta = (ui32)floor(copper / 100.0);
		silver += delta;
		copper -= delta * 100;
		delta = (ui32)floor(silver / 100.0);
		gold += delta;
		silver -= delta * 100;
		return (gold * 10000 + silver * 100 + copper);
	}

	Useable::Money ConvertToMoney(ui32 money)
	{
		float tempval = (float)money;
		tempval /= 10000.0f;
		ui32 gold = (ui32)floor(tempval);
		money -= gold * 10000;
		tempval = (float)money;
		tempval /= 100.0f;
		ui32 silver = (ui32)floor(tempval);
		money -= silver * 100;
		ui32 copper = money;
		Money mon;
		mon.gold = gold;
		mon.silver = silver;
		mon.copper = copper;
		return mon;
	}
};
